package io.renren.modules.diycake.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import io.renren.common.exception.RRException;
import io.renren.modules.diycake.dao.BannerDao;
import io.renren.modules.diycake.entity.BannerEntity;
import io.renren.modules.diycake.entity.VedioEntity;
import io.renren.modules.diycake.service.OssService;
import io.renren.modules.diycake.service.VedioService;
import io.renren.modules.diycake.utils.ConstantPropertiesUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;

@Service
public class OssServiceImpl implements OssService {
  @Autowired(required = false)
  private BannerDao bannerMapper;
  @Autowired
  private VedioService vedioService;
  @Override
  public String uploadFileAvatar(MultipartFile file) {
    try {
      //获取阿里云存储相关常量
      String endPoint = ConstantPropertiesUtils.END_POINT;
      String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
      String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
      String bucketName = ConstantPropertiesUtils.BUCKET_NAME;
      OSS ossClient = new OSSClientBuilder().build(endPoint,accessKeyId,accessKeySecret);

      InputStream inputStream = file.getInputStream();
      String filename = file.getOriginalFilename();

      String uuid = UUID.randomUUID().toString().replaceAll("-","");
      filename = uuid + filename;

      ossClient.putObject(bucketName,filename,inputStream);
      ossClient.shutdown();
      String url = "https://" + bucketName + "." + endPoint + "/" + filename;
      BannerEntity banner = new BannerEntity();
      banner.setUrl(url);
      banner.setId(null);
      int i = bannerMapper.insert(banner);
      if (i != 1) {
        throw new RRException("图片插入失败!");
      }
      return url;
    }catch (Exception e) {
      e.printStackTrace();
      return null;
    }

  }

  @Override
  public String uploadFilePhoto(MultipartFile file) {
    try {
      //获取阿里云存储相关常量
      String endPoint = ConstantPropertiesUtils.END_POINT;
      String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
      String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
      String bucketName = ConstantPropertiesUtils.BUCKET_NAME;
      OSS ossClient = new OSSClientBuilder().build(endPoint,accessKeyId,accessKeySecret);

      InputStream inputStream = file.getInputStream();
      String filename = file.getOriginalFilename();

      String uuid = UUID.randomUUID().toString().replaceAll("-","");
      filename = uuid + filename;

      ossClient.putObject(bucketName,filename,inputStream);
      ossClient.shutdown();
      String url = "https://" + bucketName + "." + endPoint + "/" + filename;
      return url;
    }catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }

  @Override
  public String uploadOssVedioId(MultipartFile file, Integer vedioId) {
    String s = uploadFilePhoto(file);
    VedioEntity vedioEntity = new VedioEntity();
    vedioEntity.setPhotoUrl(s);
    vedioEntity.setId(vedioId);
    vedioService.updateById(vedioEntity);
    return s;
  }
}
